package com.epalmpay.controller.shop.index;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;

import com.epalmpay.dto.member.MemberOutDTO;
import com.epalmpay.entity.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.merchant.index.GroupActivityBaseOutDTO;
import com.epalmpay.dto.shopindex.MemberCountOfflineOutDTO;
import com.epalmpay.dto.shopindex.ShopIndexInDTO;
import com.epalmpay.dto.shopindex.ShopIndexListOutDTO;
import com.epalmpay.service.index.IIndexService;
/** 
 * 说明：门店首页
 * 创建人：gwn
 * 创建时间：2017-07-07
 */
@Controller
@RequestMapping(value="/shop")
public class ShopIndexController  extends BaseController{

	@Resource
	private IIndexService indexService;
	
	
	@RequestMapping(value="/index")
	@ResponseBody
	public ShopIndexListOutDTO shopIndex(ShopIndexInDTO shopIndexInDTO){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		ShopIndexListOutDTO shopIndexListOutDTO= new ShopIndexListOutDTO();
	
	try {
		
		//银行卡数据 都去掉待支付的 度去掉线上的
		TradeOffline tradeOffline = indexService.queryCardData(shopIndexInDTO);
		shopIndexListOutDTO.setTradeOffline(tradeOffline);
		//扫码付收银 微信支付宝 收款
		TradeOffline tradeOffline2 = indexService.queryScanData(shopIndexInDTO);
		shopIndexListOutDTO.setTradeOffline2(tradeOffline2);
		//钱包收银
		TradeOffline tradeOffline3 = indexService.queryWalletData(shopIndexInDTO);
		shopIndexListOutDTO.setTradeOffline3(tradeOffline3);
		//台卡收银
		TradeOffline tradeOffline4 = indexService.queryDeskCardData(shopIndexInDTO);
		shopIndexListOutDTO.setTradeOffline4(tradeOffline4);
		//今日目前收益总金额 //现金的
		TradeOffline tradeOffline7 = indexService.queryMoney(shopIndexInDTO);
		shopIndexListOutDTO.setTradeOffline7(tradeOffline7);
		//核销劵码
		OrderServiceGoodDetail orderServiceGoodDetail = indexService.queryOrderData(shopIndexInDTO);
		shopIndexListOutDTO.setOrderServiceGoodDetail(orderServiceGoodDetail);


		//近30天会员消费人数(线上线下的相加)
		TradeOffline tradeOffline6 = new TradeOffline();
		int count = indexService.queryMemberSum(shopIndexInDTO);
		tradeOffline6.setTotalCount(count);
		shopIndexListOutDTO.setTradeOffline6(tradeOffline6);
		

		//近30天发展会员人数
		Member member = new Member();
		int sum = indexService.queryDevMemberSum(shopIndexInDTO);
		member.setSum(sum);
		shopIndexListOutDTO.setMember(member);

		//近30天核销劵码 张数
//		OrderServiceGoodDetail orderServiceGoodDetail2 = new  OrderServiceGoodDetail();
//		int sum2 = indexService.querySum(shopIndexInDTO);
//		orderServiceGoodDetail2.setTotalCount(sum2);
//		shopIndexListOutDTO.setOrderServiceGoodDetail2(orderServiceGoodDetail2);
//		//近30天核销劵码 金额
//		OrderServiceGoodDetail orderServiceGoodDetail3 = indexService.queryMonthOrderServiceAmount(shopIndexInDTO);
//		shopIndexListOutDTO.setOrderServiceGoodDetail3(orderServiceGoodDetail3);

		//近30天 商城商品订单数 和订单总金额
		OrderGoodItem orderGoodItem = indexService.queryOrderGood(shopIndexInDTO);
		shopIndexListOutDTO.setOrderGoodItem(orderGoodItem);


		//最近30天的核销优惠券张数
		TradeOffline tradeOffline8=indexService.queryMonthVerifyCouponCount(shopIndexInDTO);
		shopIndexListOutDTO.setTradeOffline8(tradeOffline8);

		//最近30天发放优惠券张数
		CouponUser couponUser = indexService.querySendCouponCount(shopIndexInDTO);
		shopIndexListOutDTO.setCouponUser(couponUser);
		

		//券码核销笔数 和金额
		OrderServiceGoodDetail orderServiceGoodDetail4=indexService.queryMonthOrderServiceGoodCount(shopIndexInDTO);
		shopIndexListOutDTO.setOrderServiceGoodDetail4(orderServiceGoodDetail4);

		
		//评价信息
		StatisShop statisShop = indexService.queryStarSum(shopIndexInDTO);
		shopIndexListOutDTO.setStatisShop(statisShop);
		//服务商信息
		Shop shop = indexService.queryShopData(shopIndexInDTO);
		shopIndexListOutDTO.setShop(shop);
		
	    //参与活动
		List<GroupActivityBaseOutDTO> groupActivityBaseOutDTOList =indexService.queryShopActivity(shopIndexInDTO);
		for (GroupActivityBaseOutDTO groupActivityBaseOutDTO : groupActivityBaseOutDTOList) {
			if (groupActivityBaseOutDTO.getEndTime()!=null) {
				Long time1= groupActivityBaseOutDTO.getEndTime().getTime();
				System.out.println(time1);
				Long time2 = new Date().getTime();
				if (time1-time2>0 && time1!=253402185600000l) {
					groupActivityBaseOutDTO.setDay((int) ((time1-time2)/86400000));//得到天
				}else if (time1-time2<0  && time1!=253402185600000l) {
					groupActivityBaseOutDTO.setDay(-1);//表示已过期
				}else if( time1==253402185600000l){
					groupActivityBaseOutDTO.setDay(-2);
				}
			}else{
				groupActivityBaseOutDTO.setDay(-2);
			}
			
		}
		shopIndexListOutDTO.setActivityBaseOutDTOList(groupActivityBaseOutDTOList);
		
		//最近30天会员消费人次(线上和线下的相加)
		List<MemberCountOfflineOutDTO> tradeOfflineMemberList = indexService.queryMonthMemberConsumeCount(shopIndexInDTO);
//		shopIndexListOutDTO.setTradeOfflineMemberCountList(tradeOfflineMemberList);
		//最近会员发展人数
		List<Member> memberList = indexService.queryDevMemberCount(shopIndexInDTO);
//		shopIndexListOutDTO.setMemberList(memberList);


		List resultlist2 = new ArrayList();
		for(int i=29;i>=0;i--) {
			Map resultmap = new HashMap();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			c.add(Calendar.DATE, - i);
			Date monday = c.getTime();
			String preMonday = sdf.format(monday);
			resultmap.put("date", preMonday);
			resultmap.put("memberCount", 0);
			resultmap.put("memberDevCount",0);

			resultlist2.add(resultmap);
		}


		for(MemberCountOfflineOutDTO result:tradeOfflineMemberList){
			String datet =dateFormat.format(result.getGmtCreate());
			for(int k=0;k<resultlist2.size();k++) {
				Map map = (HashMap) resultlist2.get(k);
				int number = result.getMemberCount();
				if(map.containsValue(datet)) {//已经存在该日期
					System.out.print(datet);
					map.put("memberCount", map.get("memberCount")==null?number:Integer.parseInt(map.get("memberCount").toString()) + number);
				}

			}
		}
		for(Member result:memberList){
			String datet =dateFormat.format(result.getGmtCreate());
			for(int k=0;k<resultlist2.size();k++) {
				Map map = (HashMap) resultlist2.get(k);
				int number = result.getMemberDevCount();
				if(map.containsValue(datet)) {//已经存在该日期
					System.out.print(datet);
					map.put("memberDevCount", map.get("memberDevCount")==null?number:Integer.parseInt(map.get("memberDevCount").toString()) + number);
				}

			}
		}

		shopIndexListOutDTO.setMonthMemberData(resultlist2);

	
		//最近30天会员消费人次 和发展会员
//		List<StatisShopDay> ststisShopDayList = indexService.queryMemberMonthData(shopIndexInDTO);
//		shopIndexListOutDTO.setStatisShopDay(ststisShopDayList);
		
		//最近30天的服务订单 和订单金额
		List<OrderServiceGoodDetail> orderServiceGoodDetailList = indexService.queryOrderServiceGoodDetailData(shopIndexInDTO);
		shopIndexListOutDTO.setOrderServiceGoodDetailList(orderServiceGoodDetailList);




		List resultlist = new ArrayList();
		for(int i=29;i>=0;i--) {
			Map resultmap = new HashMap();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			c.add(Calendar.DATE, - i);
			Date monday = c.getTime();
			String preMonday = sdf.format(monday);
			resultmap.put("date", preMonday);
			resultmap.put("verifyCount", 0);
			resultmap.put("sendCount",0);

			resultlist.add(resultmap);
		}

		//最近30天的核销优惠卷数量 和已发放数量
		List<CouponUser> couponUserList = indexService.queryCouponUserData(shopIndexInDTO);
		for(CouponUser result:couponUserList){
			String datet =dateFormat.format(result.getGmtCreate());
			for(int k=0;k<resultlist.size();k++) {
				Map map = (HashMap) resultlist.get(k);
				int number = result.getCount();
				int type = result.getType();
				if(map.containsValue(datet)) {//已经存在该日期
					System.out.print(datet);
					if(type==1){//核销
						map.put("verifyCount", map.get("verifyCount")==null?number:Integer.parseInt(map.get("verifyCount").toString()) + number);
					}else{
						map.put("sendCount", map.get("sendCount")==null?number:Integer.parseInt(map.get("sendCount").toString()) + number);
					}
				}

			}
		}

		shopIndexListOutDTO.setMonthCouponSendAndVerify(resultlist);

//		shopIndexListOutDTO.setCouponUserList(couponUserList);
		
		//最近一个月的商品订单数和自建商品订单金额
//		List<OrderServiceGood> orderServiceGoodList = indexService.queryMonthOrderNum(shopIndexInDTO);
//		shopIndexListOutDTO.setOrderServiceGoodList(orderServiceGoodList);
//		//最近一个月的商品自建金额
//		List<TradeOffline> tradeOfflineList = indexService.queryMonthOrderAmount(shopIndexInDTO);
//		shopIndexListOutDTO.setTradeOfflineList(tradeOfflineList);

		//最近一个月商城商品订单 和总金额
		List<OrderGoodItem> orderGoodItemList = indexService.monthOrderGoodData(shopIndexInDTO);
		shopIndexListOutDTO.setOrderGoodItemList(orderGoodItemList);


		shopIndexListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
		return shopIndexListOutDTO;
	} 
	catch (ApplicationException ae) { 
		shopIndexListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
		ae.printStackTrace();

	}
	catch (Exception e) {
		shopIndexListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
	}
	return shopIndexListOutDTO;
	
	}
	
	
}
	
	
